<?php
/**
 * @author: Hua Phuoc Truong
 * @created: 10:30 AM 10/4/13
 * @copyright: 2013
 * @version: 1.0
 */

include_once('ZReader.php');
include_once('ZPostCollection.php');
include_once('ZPostItem.php');

class ZPostReader extends ZReader {
    /**
     * List of post id that need to retrieve information
     * @var Int[]
     */
    private     $ids;

    public function __construct($ids, $timeZone = 7) {
        $this->ids = $ids;

        parent::__construct(0, 0, $timeZone, 0, 0);
    }

    /**
     * Generate command string
     * @return string
     */
    public function command() {
        return sprintf('post %d %d %s'."\r\n",
            count($this->ids),
            $this->timeZone,
            implode(' ', (array)$this->ids)
        );
    }

    /**
     * Parse data that receive from server to object
     * @param $callback
     * @param $state
     * @return ZPostCollection
     */
    public function parse($callback, &$state) {
        $i = 0;
        $collections = new ZPostCollection();

        $arr = array();

        $succeed    = false;

        //Read data while not end of file
        while (!$this->eof()) {
            //Read the first block is the length of the data
            $numBytes = $this->readNum();
            if ($numBytes >= 0 && $numBytes < 0xffffffff) {
                if ($i < 11){
                    $arr[] = $this->readStr($numBytes);
                }
                else{
                    $arr[] = $this->readNum($numBytes);
                }
                $i++;

                //Enough a item
                if ($i == 15) {
                    $item = new ZPostItem($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7], $arr[8], $arr[9], $arr[10], $arr[11], $arr[12], $arr[13], $arr[14]);

                    $collections->addItem($item);
                    $arr = array();
                    $i = 0;

                    if (is_callable($callback)) {
                        $callback($item, 0);
                    }
                }
                //Not succeeded
                $succeed = false;
            }
            else if ($numBytes == 0xffffffff) {
                $succeed = true;
            }
        }
        $state = $succeed ? ($collections->count()> 0 ? 1 : 0) : -1;

        return $collections;
    }

    /**
     * @param null  $callback   callback function when receive data
     * @return ZPostCollection
     * @throws Exception
     */
    public function read($callback = null) {
        return parent::readData($callback);
    }

    /**
     * This function is not valid here,
     * we overload it and just raise a notice
     */
    public function setSession($ses) {
        trigger_error("Session is not make sense in ZPostReader, so any session get/set will be ignored!");
    }

    /**
     * This function is not valid here,
     * we overload it and just raise a notice
     */
    public function getSession() {
        trigger_error("Session is not make sense ZPostReader, so any session get/set will be ignored!!");
    }

    /**
     * This function is not valid here
     */
    public function getExcludedIds() {
        trigger_error("Excluded id is not make sense ZPostReader, so this function will be ignored!!");
    }

    /**
     * This function is not valid here
     */
    public function setExcludedIds($ids) {
        trigger_error("Excluded id is not make sense ZPostReader, so this function will be ignored!!");
    }

    /**
     * This function is not valid here
     */
    public function getExcludedThreadIds() {
        trigger_error("Excluded thread id is not make sense ZPostReader, so this function will be ignored!!");
    }

    /**
     * This function is not valid here
     */
    public function setExcludedThreadIds($ids) {
        trigger_error("Excluded thread id is not make sense ZPostReader, so this function will be ignored!!");
    }
}